﻿using System;
using MyFloridaHouse.libSTO;
using MyFloridaHouse.DataAccess;
using MyFloridaHouse.libSvCommonUtilities;
using MyFloridaHouse.libSvDTO;

namespace MyFloridaHouse.libServiceBusinessRules
{
    public static class DistrictPlan
    {
		/// <summary>
		/// Gets the district plan. Data is store in blob storage.
		/// The blob is cached on the local user's isolated storage in Silverlight.  
		/// Thus if the object is present and still current it is not loaded again.
		/// A ref is appended so that the cached name can be compared with the current storage version.
		/// </summary>
		/// <param name="iDistrictPlanFileName">Name of the i district plan file.</param>
		/// <returns>Object: <see cref="DistictPlanSTO"/></returns>
        public static DistictPlanSTO GetDistrictPlan(string iDistrictPlanFileName)
        {
            DistictPlanSTO results = new DistictPlanSTO();
            try
            {
                DAL_CensusData dal = new DAL_CensusData();
                //get constant for DistictPlan

                string currentDistrictPlanKey = dal.GetCurrentConfigKey(Constants.DefaultDistrictPlanFileKey);
                if (currentDistrictPlanKey == iDistrictPlanFileName) //isolated storage is up to date
                {
                    results.isCurrent = true;
                }
                else //isolated storage NOT up to date get it from Blob
                {
                    results.isCurrent = false;
                    FileInfoDTO fileInfoDTO = new FileInfoDTO();
                    fileInfoDTO.fileName = currentDistrictPlanKey;
                    fileInfoDTO.url = Constants.BlobURL;
                    FileSTO file = new FileSTO();
                    file = File.GetBlobBinaryFile(fileInfoDTO);
                    results.districtPlan = file.fileData;
                }
                results.districtPlanFileName = currentDistrictPlanKey;
                return results;
            }
            catch (Exception ex)
            {
                results.errorMessage = ex.Message;
                return results;
            }
        }
    }
}
